package utils

import (
	"encoding/binary"
	"os"

	"github.com/BurntSushi/toml"
)

type GlobalObj struct {
	ByteOrder        binary.ByteOrder
	Host             string `toml:"host"`                //当前服务器主机IP
	Port             int    `toml:"port"`                //当前服务器主机监听端口号
	Name             string `toml:"name"`                //当前服务器名称
	Version          string `toml:"version"`             //当前Zinx版本号
	MaxPacketSize    uint32 `toml:"max_packet_size"`     //都需数据包的最大值
	MaxConn          int    `toml:"max_conn"`            //当前服务器主机允许的最大链接个数
	WorkerPoolSize   uint32 `toml:"worker_pool_size"`    //业务工作Worker池的数量
	MaxWorkerTaskLen uint32 `toml:"max_worker_task_len"` //业务工作Worker对应负责的任务队列最大任务存储数量
	MaxMsgChanLen    uint32 `toml:"max_msg_chan_len"`    //SendBuffMsg发送消息的缓冲最大长度
}

var GlobalObject *GlobalObj
var config_file = "gitee.com/colin0724/zserver.toml"

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func (g *GlobalObj) Reload() {
	if confFileExists, _ := PathExists(config_file); confFileExists != true {
		return
	}

	_, err := toml.DecodeFile(config_file, &g)
	if err != nil {
		panic(err)
	}
}

func init() {
	GlobalObject = &GlobalObj{
		ByteOrder:        binary.BigEndian,
		Name:             "gitee.com/colin0724/zserver",
		Version:          "V0.1",
		Port:             8999,
		Host:             "0.0.0.0",
		MaxConn:          12000,
		MaxPacketSize:    4096,
		WorkerPoolSize:   10,
		MaxWorkerTaskLen: 1024,
		MaxMsgChanLen:    1024,
	}

	GlobalObject.Reload()
}
